#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Any, Optional

from autoapi.common.log import log


class BaseDB(ABC):
    """基础数据库接口类"""
    
    @abstractmethod
    def init(self) -> None:
        """初始化数据库连接"""
        pass

    @abstractmethod
    def get(self, name: Any, *args, **kwargs) -> Any:
        """获取数据"""
        pass

    @abstractmethod
    def set(self, name: Any, value: Any, *args, **kwargs) -> Any:
        """设置数据"""
        pass

    @abstractmethod
    def rset(self, key: Any, value: Any, *args, **kwargs) -> Any:
        """重置设置数据"""
        pass

    @abstractmethod
    def delete(self, name: Any, *args, **kwargs) -> Any:
        """删除数据"""
        pass

    @abstractmethod
    def get_prefix(self, prefix: str) -> list:
        """获取符合前缀的所有数据"""
        pass

    @abstractmethod
    def delete_prefix(self, prefix: str, exclude: Optional[str] = None) -> None:
        """删除符合前缀的所有数据"""
        pass

    def _log_operation(self, operation: str, success: bool, message: str = "") -> None:
        """记录数据库操作日志"""
        status = "成功" if success else "失败"
        log_msg = f"数据库{operation}操作{status}"
        if message:
            log_msg += f": {message}"
        
        if success:
            log.info(log_msg)
        else:
            log.error(log_msg)
